package flst.ipiweb.persistance.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import flst.ipiweb.persistance.interfaces.IClientDAL;
import flst.ipiweb.transverse.Client;
import flst.ipiweb.transverse.IPIWebException;

public class ClientJdbcDAL implements IClientDAL{

	
	public Client GetClientById(int id) throws IPIWebException {

		Client cli = new Client();
		Connection maconnection;

		try {
			maconnection = JdbcUtils.getInstance().getConnection();
			PreparedStatement req = maconnection.prepareStatement("SELECT NOM, PRENOM, LOGIN, MDP, CIVILITE, ADRESSE, CPLTADRESSE, CODEPOSTAL, VILLE, TELEPHONE, EMAIL FROM UTILISATEUR WHERE IDUTILISATEUR = ? AND (TYPE = 3 OR TYPE = 4);");
			req.setInt(1, id);
			req.execute();

			ResultSet rs = req.getResultSet();
			while (rs.next()) {
				
					cli.setNom(rs.getString(1));
					cli.setPrenom(rs.getString(2));
					cli.setLogin(rs.getString(3));
					cli.setMotDePasse(rs.getString(4));
					cli.setCivilite(rs.getString(5));
					cli.setAdresse(rs.getString(6));
					cli.setCpltAdresse(rs.getString(7));
					cli.setCodePostal(rs.getInt(8));
					cli.setVille(rs.getString(9));
					cli.setTelephone(rs.getString(10));
					cli.setEmail(rs.getString(11));
					cli.setId(id);
					
			}

			req.close();
			maconnection.close();
		} catch (Exception e) {
			throw new IPIWebException(e.getMessage());
		}
		return cli;

	}

}
